﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;

namespace freecart
{
    public class BaseClass : System.Web.UI.Page
    {
        private bool _debugMode = false;
        public bool DebugMode { get { return this._debugMode; } set { this._debugMode = value; } }

        protected override void OnLoad(EventArgs e) {
            if (!Page.IsPostBack)
            {
                ConnectionString.String = ConfigurationManager.ConnectionStrings["Freecart"].ConnectionString;

                Freecart cart = new Freecart();
                cart.Theme = ConfigurationManager.AppSettings["Theme"];

                if (String.IsNullOrEmpty(cart.Theme))
                {
                    cart.Theme = "Default";
                }

                if (Context.Session["SessionId"] == null)
                {
                    cart.SessionId = System.Guid.NewGuid();
                    Context.Session["SessionId"] = cart.SessionId;
                }
                else {
                    cart.SessionId = new Guid(Context.Session["SessionId"].ToString());
                }

                if (checkDebug(ConfigurationManager.AppSettings["DebugMode"]))
                {
                    this.Page.Title += " (debug mode)";
                    this.DebugMode = true;
                }


                LiteralControl Preload = new LiteralControl();
                Preload.Text = "<div id=\"Preload\">" + PreLoadFiles(cart.Theme).ToString() + "</div>";
                Page.Controls.Add(Preload);

                LiteralControl lit = new LiteralControl();
                lit.Text = "<link media=\"screen\" href=\"./Themes/" + cart.Theme + "/Stylesheet.css\" rel=\"Stylesheet\" />";
                Header.Controls.Add(lit);

                if (CartManager.Carts.Select("SessionId = '" + cart.SessionId + "'").Length == 0)
                {
                    if (this.DebugMode == true)
                    {
                        if (CartManager.Carts != null)
                        {
                            if (CartManager.Carts.Select("SessionId = '" + cart.SessionId + "'").Length <= 0)
                            {
                                cart.SampleTable(cart.SessionId);
                            }
                        }
                    }
                }

                if (this.DebugMode == true)
                {
                    Context.Response.Write("Session ID: " + cart.SessionId);
                }
            }

            base.OnLoad(e);
        }

        private bool checkDebug(string AppSetting)
        {
            if (!String.IsNullOrEmpty(AppSetting) && AppSetting.Equals("true"))
            {
                return true;
            }
            return false;
        }

        private StringBuilder PreLoadFiles(string ThemeName)
        {
            StringBuilder sb = new StringBuilder();
            DirectoryInfo di = new DirectoryInfo(Server.MapPath(".") + "/Themes/" + ThemeName + "/Images/");
            FileInfo[] rgFiles = di.GetFiles("*");
            foreach (FileInfo fi in rgFiles)
            {
                sb.Append("<img src=\"./Themes/" + ThemeName + "/Images/" + fi.Name + "\">");
            }
            return sb;
        }
    }
}
